package huawei.js2025;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 静态扫描
 */
public class Question42 {

    public static class FileInfo{
        private int fileId;
        private int fileSize;
        private int fileFre;

        public FileInfo(int fileId, int fileSize){
            this.fileId = fileId;
            this.fileSize = fileSize;
            this.fileFre = 1;
        }

        public int getTotal() {
            return fileSize * fileFre;
        }

        public int getBufTotal(int M) {
            return fileSize + M;
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int M = sc.nextInt();
        sc.nextLine();

        List<Integer> fileIds = Arrays.stream(sc.nextLine().split(" ")).map(String::trim).map(Integer::parseInt).collect(Collectors.toList());
        List<Integer> fileSizes = Arrays.stream(sc.nextLine().split(" ")).map(String::trim).map(Integer::parseInt).collect(Collectors.toList());

        Map<Integer, FileInfo> mapFreMap = new HashMap<>(); // 文件id出现的次数
        for (int i = 0; i < fileIds.size(); i++) {
            int fileId = fileIds.get(i);
            int fileSize = fileSizes.get(i);

            if (mapFreMap.containsKey(fileId)) {
                FileInfo fre = mapFreMap.get(fileId);
                fre.fileFre += 1;
            } else {
                FileInfo fileInfo = new FileInfo(fileId, fileSize);
                mapFreMap.put(fileId, fileInfo);
            }
        }

        int totalSum = 0; // 总成本

        for (Integer fileId : mapFreMap.keySet()) {
            FileInfo fileInfo = mapFreMap.get(fileId);
            if (fileInfo.getTotal() > fileInfo.getBufTotal(M)) {
                totalSum += fileInfo.getBufTotal(M);
            } else {
                totalSum += fileInfo.getTotal();
            }
        }

        System.out.println(totalSum);

    }
}
